package com.fjnu.controller.repair;

import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.fjnu.controller.common.BaseController;
import com.fjnu.dao.IFinalRepairDao;
import com.fjnu.entities.ExamRepair;
import com.fjnu.entities.FinalRepair;
import com.fjnu.entities.User;
import com.fjnu.service.IExamRepairService;
import com.fjnu.service.IFinalRepairService;
import com.fjnu.service.impl.FinalRepairService;
@Controller
public class ExamRepairController extends BaseController{
	private static Logger logger =LogManager.getLogger(ExamRepairController.class);
	@Autowired
	private IExamRepairService examRepairService;
	@Autowired
	private IFinalRepairService finalRepairService;
	@RequiresRoles("后勤管理员")
	@RequestMapping(value="/findExamRepair")
	@ResponseBody
	public Map<String,Object> findExamRepair(Integer pageNo,Integer pageSize){
		logger.info("遍历报修单");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		if(pageSize==null){
			pageSize=10;
		}
		if(pageNo==null){
			pageNo=1;
		}
		List<ExamRepair> list = examRepairService.findExamRepair(pageNo, pageSize); 
		put("data",list);
		return getData2Result();
	}
	/**
	 * 审核受理一份报修单
	 * @param id
	 * @param status 1:审核通过  4:审核不通过
	 * @return
	 */
	@RequiresRoles("后勤管理员")
	@RequestMapping(value="/ExamRepair")
	@ResponseBody
	public Map<String,Object> ExamRepair(Integer id,Integer status){
		logger.info("审核一份报修单");
		if(!init(id,status).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		 Session session = currentUser.getSession();
		User user=(User) session.getAttribute("subject");
		ExamRepair examRepair = examRepairService.findUniqueExamRepair(id);
		examRepair.getBaseRepair().setStatus(status);
		examRepair.setExamUser(user);
		examRepairService.updateExamRepair(examRepair);
		
		if(status==1){
			logger.info("审核通过，上报维修部门");
			FinalRepair finalRepair = new FinalRepair(null, examRepair);
			finalRepairService.addFinalRepair(finalRepair);
			}
		return getData2Result();
	}
}
